package com.google.helloworld;

import java.util.ArrayList;
import java.util.List;

import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;


public class ViewMapActivity extends com.google.android.maps.MapActivity
							 implements LocationListener {
	private final String TAG = "ViewMapActivity";
	private final int ONE_SECOND = 1000;
	private final int KILOMETER = 1000;
	
	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.map);
		
		LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
		lm.requestSingleUpdate(LocationManager.GPS_PROVIDER, this, getMainLooper());
		
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	
	private class AndroidOverlay extends com.google.android.maps.ItemizedOverlay{

		public AndroidOverlay(Drawable defaultMarker) {
			  super(boundCenterBottom(defaultMarker));
		}
		
		public void addOverlay(OverlayItem overlay) {
		    mOverlays.add(overlay);
		    populate();
		}
		
		@Override
		protected OverlayItem createItem(int i) {
			  return mOverlays.get(i);
		}
		
		@Override
		public int size() {
		  return mOverlays.size();
		}
		
	};

	public void onLocationChanged(Location location) {
		Log.d(TAG, "Lat: "+String.valueOf(location.getLatitude()) 
				+" Long: "+String.valueOf(location.getLongitude()));
		
		// Get reference to map
		MapView mapView = (MapView) findViewById(R.id.mapview);
		
		// Setup overlay with our image
		Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
		AndroidOverlay overlay = new AndroidOverlay(drawable);
		
		// Create geopoint from GPS location
		GeoPoint point = new GeoPoint((int) (location.getLatitude() * 1e6),
									   (int) (location.getLongitude() * 1e6));
		
		// Create overlay and add to map
		OverlayItem me = new OverlayItem(point, "Howdy!", "Here I am!");
		overlay.addOverlay(me);
		mapView.getOverlays().clear();
		mapView.getOverlays().add(overlay);
	}

	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}

}
